package cn.heyige.backend.controller;

import cn.heyige.backend.common.CommResponse;
import cn.heyige.backend.dto.*;
import cn.heyige.backend.service.UnifiedUserService;
import cn.heyige.backend.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("api/auth")
@Slf4j
@RequiredArgsConstructor
@Tag(name = "用户认证服务", description = "用户认证相关接口")
public class AuthController {
    
    private final UnifiedUserService unifiedUserService;
    private final JwtUtil jwtUtil;
    
    
    @PostMapping("/send-code")
    @Operation(summary = "发送短信验证码")
    public CommResponse<SendCodeResponse> sendCode(@Valid @RequestBody SendCodeRequest request) {
        try {
            SendCodeResponse response = unifiedUserService.sendVerificationCode(request);
            return CommResponse.ok(response);
        } catch (Exception e) {
            log.error("发送验证码失败", e);
            return CommResponse.fail(400, e.getMessage());
        }
    }
    
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public CommResponse<UserAuthResponse> register(@Valid @RequestBody UserRegistrationRequestV2 request) {
        try {
            UserAuthResponse response = unifiedUserService.registerWithPhone(request);
            return CommResponse.ok(response);
        } catch (Exception e) {
            log.error("用户注册失败", e);
            return CommResponse.fail(400, e.getMessage());
        }
    }
    
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public CommResponse<UserAuthResponse> login(@Valid @RequestBody UserLoginRequestV2 request) {
        try {
            UserAuthResponse response = unifiedUserService.loginWithPhone(request);
            return CommResponse.ok(response);
        } catch (Exception e) {
            log.error("用户登录失败", e);
            return CommResponse.fail(400, e.getMessage());
        }
    }
    
    @PostMapping("/refresh")
    @Operation(summary = "刷新访问令牌")
    public CommResponse<RefreshTokenResponse> refresh(@Valid @RequestBody RefreshTokenRequest request) {
        try {
            RefreshTokenResponse response = unifiedUserService.refreshToken(request.getRefreshToken());
            return CommResponse.ok(response);
        } catch (Exception e) {
            log.error("刷新令牌失败", e);
            return CommResponse.fail(401, "刷新令牌失败");
        }
    }
    
    @PostMapping("/logout")
    @Operation(summary = "用户登出")
    @Parameter(name = "Authorization", description = "Bearer Token", required = true)
    public CommResponse<Map<String, Object>> logout(
            @RequestHeader("Authorization") String authorization,
            @RequestBody(required = false) LogoutRequest request) {
        try {
            //String token = jwtUtil.extractTokenFromHeader(authorization);
            String token = "";
            if (token == null) {
                return CommResponse.fail(401, "无效的Authorization头");
            }
            
            Boolean allDevices = request != null ? request.getAllDevices() : false;
            unifiedUserService.logout(token, allDevices);
            
            return CommResponse.ok(Map.of());
        } catch (Exception e) {
            log.error("用户登出失败", e);
            return CommResponse.fail(400, e.getMessage());
        }
    }
}